Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@react-stately/collections

Package Overview
Dependencies
Maintainers
2
Versions
851
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@react-stately/collections

Spectrum UI components in React

  • 3.12.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.4M
decreased by-8.32%
Maintainers
2
Weekly downloads
 
Created

What is @react-stately/collections?

@react-stately/collections is a library that provides utilities for managing collections of data in React applications. It offers a set of hooks and components to handle common collection operations such as iteration, filtering, and sorting in a declarative way.

What are @react-stately/collections's main functionalities?

Iterating over collections

This feature allows you to iterate over a collection of items and render them in a list. The `useCollection` hook is used to manage the collection state.

import { useCollection } from '@react-stately/collections';

const items = [
  { id: 1, name: 'Item 1' },
  { id: 2, name: 'Item 2' },
  { id: 3, name: 'Item 3' }
];

function MyComponent() {
  const collection = useCollection({ items });

  return (
    <ul>
      {collection.map(item => (
        <li key={item.id}>{item.name}</li>
      ))}
    </ul>
  );
}

Filtering collections

This feature allows you to filter a collection of items based on a condition. The `filter` method is used to create a new array with items that match the condition.

import { useCollection } from '@react-stately/collections';

const items = [
  { id: 1, name: 'Item 1' },
  { id: 2, name: 'Item 2' },
  { id: 3, name: 'Item 3' }
];

function MyComponent() {
  const collection = useCollection({ items });
  const filteredItems = collection.filter(item => item.name.includes('1'));

  return (
    <ul>
      {filteredItems.map(item => (
        <li key={item.id}>{item.name}</li>
      ))}
    </ul>
  );
}

Sorting collections

This feature allows you to sort a collection of items based on a comparison function. The `sort` method is used to order the items in the collection.

import { useCollection } from '@react-stately/collections';

const items = [
  { id: 3, name: 'Item 3' },
  { id: 1, name: 'Item 1' },
  { id: 2, name: 'Item 2' }
];

function MyComponent() {
  const collection = useCollection({ items });
  const sortedItems = collection.sort((a, b) => a.name.localeCompare(b.name));

  return (
    <ul>
      {sortedItems.map(item => (
        <li key={item.id}>{item.name}</li>
      ))}
    </ul>
  );
}

Other packages similar to @react-stately/collections

FAQs

Package last updated on 21 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc